package com.itheima.web;


import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/report")
public class ReportController {


    @Reference
    ReportService reportService;


    //获取运营数据
    @GetMapping("/getBusinessReportData")
    @PreAuthorize("hasAuthority('admin')")
    public Result getBusinessReportData(){
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(map);
    }

    @GetMapping("/exportBusinessReport")
    @PreAuthorize("hasAuthority('admin')")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response) {

        try {
            Map<String, Object> data = reportService.getBusinessReportData();
            //获取当前日期
            String reportDate = (String) data.get("reportDate");
            //今天新增会员数
            Integer todayNewMember = (Integer) data.get("todayNewMember");
            //总会员数
            Integer totalMember = (Integer) data.get("totalMember");
            //本周新增会员数
            Integer thisWeekNewMember = (Integer) data.get("thisWeekNewMember");
            //本月新增会员数
            Integer thisMonthNewMember = (Integer) data.get("thisMonthNewMember");
            //今日预约数
            Integer todayOrderNumber = (Integer) data.get("todayOrderNumber");
            //本周预约数
            Integer thisWeekOrderNumber = (Integer) data.get("thisWeekOrderNumber");
            //本月预约数
            Integer thisMonthOrderNumber = (Integer) data.get("thisMonthOrderNumber");
            //今日到诊数
            Integer todayVisitsNumber = (Integer) data.get("todayVisitsNumber");
            //本周到诊数
            Integer thisWeekVisitsNumber = (Integer) data.get("thisWeekVisitsNumber");
            //本月到诊数
            Integer thisMonthVisitsNumber = (Integer) data.get("thisMonthVisitsNumber");
            //热门套餐(取前3)
            List<Map> hotSetmeal = (List<Map>) data.get("hotSetmeal");
//            Double totalProportion = (Double)data.get("totalProportion");
            BigDecimal o = (BigDecimal) data.get("totalProportion");
            Double totalProportion = o.doubleValue();

//            String filePath = request.getSession().getServletContext().getRealPath("template") + File.separator + "report_template.xlsx";
//
//            //创建工作簿
//            XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File(filePath)));
            XSSFWorkbook excel = new XSSFWorkbook("F:\\report_template.xlsx");

            XSSFSheet sheet = excel.getSheetAt(0);

            //日期
            XSSFRow row = sheet.getRow(2);
            row.getCell(5).setCellValue(reportDate);

            //新增会员数（本日）
            row = sheet.getRow(4);
            row.getCell(5).setCellValue(todayNewMember);
            //总会员数
            row.getCell(7).setCellValue(totalMember);

            //本周新增会员数
            row = sheet.getRow(5);
            row.getCell(5).setCellValue(thisWeekNewMember);
            //本月新增会员数
            row.getCell(7).setCellValue(thisMonthNewMember);

            //今日预约数
            row = sheet.getRow(7);
            row.getCell(5).setCellValue(todayOrderNumber);
            //今日到诊数
            row.getCell(7).setCellValue(todayVisitsNumber);

            //本周预约数
            row = sheet.getRow(8);
            row.getCell(5).setCellValue(thisWeekOrderNumber);
            //本周到诊数
            row.getCell(7).setCellValue(thisWeekVisitsNumber);

            //本月预约数
            row = sheet.getRow(9);
            row.getCell(5).setCellValue(thisMonthOrderNumber);
            //本月到诊数
            row.getCell(7).setCellValue(thisMonthVisitsNumber);

            //热门套餐
            int rowNum = 12;
            for (Map map : hotSetmeal) {
                String name = (String) map.get("name");
                Long setmealCount = (Long) map.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map.get("proportion");
                row = sheet.getRow(rowNum++);
                row.getCell(4).setCellValue(name);
                row.getCell(5).setCellValue(setmealCount);
                row.getCell(6).setCellValue(proportion.doubleValue()+"%");
            }
            row = sheet.getRow(15);
            row.getCell(6).setCellValue(totalProportion.doubleValue()+"%");

            OutputStream out = response.getOutputStream();
            //设置文件类型
            response.setContentType("application/vnd.ms-excel");//代表的是Excel文件类型
            //设置以附件形式进行下载
            response.setHeader("content-Disposition", "attachment;filename=report.xlsx");//指定以附件形式下载
            excel.write(out);
            out.flush();
            out.close();
            excel.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,null);
        }

    }

}
